package com.example.big_orange2.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.big_orange2.common.Result;
import com.example.big_orange2.entity.pagedata_r;
import com.example.big_orange2.entity.rechargeorder;
import com.example.big_orange2.mapper.rechargeorderMapper;
import javafx.util.Pair;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RechargeOrderService {
    @Resource
    com.example.big_orange2.mapper.rechargeorderMapper rechargeorderMapper ;

    public Result<?> findbysum(){
        LambdaQueryWrapper<rechargeorder> wrapper1 = Wrappers.<rechargeorder>lambdaQuery();
        wrapper1.eq(rechargeorder::getChannels,"Wechat");//多条件查询
        Integer useNum=rechargeorderMapper.selectCount(wrapper1);
        LambdaQueryWrapper<rechargeorder> wrapper2 =Wrappers.<rechargeorder>lambdaQuery();
        wrapper2.eq(rechargeorder::getChannels,"Alipay");//多条件查询
        Integer avaibleUseNum=rechargeorderMapper.selectCount(wrapper2);
        LambdaQueryWrapper<rechargeorder> wrapper3 =Wrappers.<rechargeorder>lambdaQuery();
        Integer totalUseNum=rechargeorderMapper.selectCount(wrapper3);
        List<Pair<String,Integer>> Loanhandle=new ArrayList<Pair<String,Integer>>();
        Pair<String,Integer> s1=new Pair("Alipay",avaibleUseNum);
        Loanhandle.add(s1);
        s1=new Pair("Wechat",useNum);
        Loanhandle.add(s1);
        s1=new Pair("totalNum",totalUseNum);
        Loanhandle.add(s1);
        return  Result.success(Loanhandle);//返回结果
    }

    public Result<?> findPage(Integer page, Integer limit, String s){
        pagedata_r ru=new pagedata_r();
        LambdaQueryWrapper<rechargeorder> wrapper =Wrappers.<rechargeorder>lambdaQuery();
        if(StrUtil.isNotBlank(s)){//判断search不是空
            wrapper.like(com.example.big_orange2.entity.rechargeorder::getRechargeid,Integer.parseInt(s));

        }
        Page<rechargeorder> ClientPage=rechargeorderMapper.selectPage(new Page<>(page,limit),wrapper);
        //ClientPage.getTotal();
        //?是泛型，表示任意类型;pageNum:前台第几页；pageSize：前台每页多少条数据；search：前台要搜索的内容;defaltvalue:默认值
        //new Page<>(pageNum,pageSize);     MybatisPlusConfig提供的分页对象。
        //Client::四个点可以直接访问对象的属性; like模糊查询;
        ru.setRechargeorderPage(ClientPage);
        ru.setTotal(ClientPage.getTotal());
        return  Result.success(ru);//返回结果

    }

    public void addmoney(Integer id, Float amount){
        Map<String, Object> mapSql = new HashMap<>();
        mapSql.put("uid", id);
        mapSql.put("amount",amount);
        rechargeorderMapper.payin(mapSql);

    }

}
